package com.github.cakin.shiro.chapter23.app1.web.exception;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;

/**
 * @className: DefaultExceptionHandler
 * @description: 异常处理
 * @date: 2020/6/2
 * @author: cakin
 */
@ControllerAdvice
public class DefaultExceptionHandler {
/**
 * 功能描述：处理UnauthorizedException异常
 *
 * @author cakin
 * @date 2020/6/2
 * @param request 请求
 * @param e 异常
 * @return ModelAndView 返回模型视图
 * @description:
 */
    @ExceptionHandler({UnauthorizedException.class})
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ModelAndView processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("exception", e);
        mv.setViewName("unauthorized");
        return mv;
    }
}
